package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.sdsmdg.tastytoast.TastyToast;

public class CustomToastActivity extends AppCompatActivity implements View.OnClickListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_custom_toast);
        Button button1 = findViewById(R.id.toast_button1);
        Button button2 = findViewById(R.id.toast_button2);
        Button button3 = findViewById(R.id.toast_button3);
        Button button = findViewById(R.id.toast_button);
        Button button5 = findViewById(R.id.toast_button5);
        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);
        button.setOnClickListener(this);
        button5.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Toast toast =null;
        switch (v.getId()){
            case R.id.toast_button1:
                 Toast.makeText(getApplicationContext(),"默认Toast",Toast.LENGTH_SHORT).show();
                break;
            case R.id.toast_button2:
                toast = Toast.makeText(getApplicationContext(),"自定义位置的Toast",Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.TOP,-100,100);
                toast.show();
                break;
            case R.id.toast_button3:
                toast = Toast.makeText(getApplicationContext(),"带图片的Toast",Toast.LENGTH_SHORT);
                LinearLayout toastView = (LinearLayout) toast.getView();
                ImageView img = new ImageView(getApplicationContext());
                img.setImageResource(R.drawable.aaaf2);
                toastView.addView(img);
                toast.setGravity(Gravity.TOP,0,100);
                toast.show();
                break;
            case R.id.toast_button:
                LayoutInflater inflater = getLayoutInflater();
                View layout = inflater.inflate(R.layout.custom_toast,(ViewGroup) findViewById(R.id.llToast));
                TextView title = layout.findViewById(R.id.tvTitleToast);
                title.setText("CustomToast");
                ImageView image = layout.findViewById(R.id.tvImageToast);
                image.setImageResource(R.drawable.aaaf1);
                TextView textView = layout.findViewById(R.id.tvTextView);
                textView.setText("这是自定义的Toast的内容部分");
                toast = new Toast(getApplicationContext());
                toast.setGravity(Gravity.RIGHT|Gravity.TOP,12,40);
                toast.setDuration(Toast.LENGTH_LONG);
                toast.setView(layout);
                toast.show();
                break;
            case R.id.toast_button5:
//                Toast.makeText(getApplicationContext(),"默认Toast",Toast.LENGTH_SHORT).show();
                TastyToast.makeText(getApplicationContext(), "Hello World !", TastyToast.LENGTH_LONG, TastyToast.WARNING);
                break;
        }
    }
}